<?php


namespace App\Controller\Admin;

use App\Annotation\OperationLog;
use \App\Controller\AbstractController;
use App\Request\Admin\CooperativeAuditIndexRequest;
use App\Request\Admin\CooperativeAuditRequest;
use App\Request\Admin\CooperativeIndexRequest;
use App\Request\Admin\CooperativeSetMenusRequest;
use App\Request\Admin\CooperativeSubmitAuditRequest;
use App\Service\Admin\CooperativeService;
use App\Utils\Upload\Server;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;


class CooperativeController extends AbstractController
{
    /**
     * @Inject()
     * @var CooperativeService
     */
    protected $cooperativeService;

    /**
     * @OperationLog(event="获取合作商列表",platform="admin")
     * @param CooperativeIndexRequest $request
     * @return mixed|ResponseInterface
     */
    public function index(CooperativeIndexRequest $request)
    {
        $validated = $request->validated();
        $paginate = $this->cooperativeService->index($validated);
        return $this->response->success($paginate);
    }

    /**
     * @OperationLog(event="冻结合作商",platform="admin")
     * @param int $cooperativeId
     * @return mixed|ResponseInterface
     */
    public function block(int $cooperativeId)
    {
        $this->cooperativeService->block($cooperativeId);
        return $this->response->success();
    }


    /**
     * @OperationLog(event="解冻合作商",platform="admin")
     * @param int $cooperativeId
     * @return mixed|ResponseInterface
     */
    public function unblock(int $cooperativeId)
    {
        $this->cooperativeService->unblock($cooperativeId);
        return $this->response->success();
    }

    /**
     * 设置合作权限
     * @OperationLog(event="设置合作权限",platform="admin")
     * @param int $id
     * @param CooperativeSetMenusRequest $request
     * @return mixed|ResponseInterface
     * @throws \Exception
     */
    public function setMenus(int $id, CooperativeSetMenusRequest $request)
    {
        $validated = $request->validated();
        $this->cooperativeService->setMenus($id, $validated['menu_config_ids']);
        return $this->response->success();
    }

    /**
     * 获取合作商权限
     * @OperationLog(event="获取合作商权限",platform="admin")
     * @param int $id
     * @return mixed|ResponseInterface
     */
    public function getMenus(int $id)
    {
        var_dump("获取合作商权限");
        var_dump($id);
        $data = $this->cooperativeService->getMenus($id);
        return $this->response->success($data);
    }

    /**
     * 获取树形结构菜单
     * @OperationLog(event="获取树形结构菜单",platform="admin")
     */
    public function menusTree()
    {
        return $this->response->success(config('menus.cooperative'));
    }

    public function auditIndex(CooperativeAuditIndexRequest $request)
    {
        $index = $this->cooperativeService->auditIndex($request->validated());
        return $this->response->success($index);
    }

    /**
     * 合作商审核
     * @OperationLog(event="合作商审核", platform="admin")
     * @param int $id
     * @param CooperativeAuditRequest $request
     * @return ResponseInterface
     */
    public function audit(int $id, CooperativeAuditRequest $request)
    {
        $validated = $request->validated();
        $data = $this->cooperativeService->audit($id, $validated);
        return $this->response->success($data);
    }

    /**
     * @OperationLog(event="提交小程序版本审核", platform="admin")
     * @param $id
     * @param CooperativeSubmitAuditRequest $request
     * @return ResponseInterface
     */
    public function submitAudit($id, CooperativeSubmitAuditRequest $request)
    {
        $validated = $request->validated();
        $data = $this->cooperativeService->submitAudit($id, (int)$validated['template_id']);
        return $this->response->success($data);
    }

    /**
     * APi证书下载
     * @OperationLog(event="APi证书下载",platform="cooperative")
     * @param string $id
     * @return mixed
     */
    public function downloadApi(string $id)
    {
        return di(Server::class)->view($id)->downloadFile();
    }

}